#include "MainWindow.h"
#include "./ui_MainWindow.h"
#include "AboutDialog.h"
#include "PauseMenuWidget.h"
#include "StartMenuWidget.h"
#include "GameWidget.h"
#include "battle.h"
#include"GlobalObject.h"
#include "MainWindow.h"         // 必须包含对应的头文件
#include <QUrl>                 // 用于QMediaPlayer的媒体路径
#include <QMediaPlayer>         // 确保QMediaPlayer类可用
#include <QAudioOutput>

using namespace MyNS;



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , startMenu(nullptr)
    , pauseMenu(nullptr)
    , aboutDialog(nullptr)
    , gameWidget(nullptr)
    ,battleWidget(nullptr)
    ,bgmPlayer(new QMediaPlayer(this))//初始化音频播放器
    ,audioOutput(new QAudioOutput(this))//初始化音频输出
{
    ui->setupUi(this);

    //设置音乐播放
    bgmPlayer->setAudioOutput(audioOutput);
    bgmPlayer->setSource(QUrl("qrc:/bgm/music/bgm/Junichi.ogg"));
    bgmPlayer->setLoops(QMediaPlayer::Infinite);
    bgmPlayer->play();

    initPokemon();
    // 创建子界面
    createWidgets();

    // 连接信号槽
    connectSignals();

    // 初始显示开始菜单
    ui->stackedWidget->setCurrentWidget(startMenu);

}

MainWindow::~MainWindow()
{
    //停止音乐
    bgmPlayer->stop();

    delete bgmPlayer;
    delete audioOutput;
    delete ui;
}

void MainWindow::createWidgets()
{
    // 创建各个界面
    startMenu = new StartMenuWidget(this);
    pauseMenu = new PauseMenuWidget(this);
    aboutDialog = new AboutDialog(this);
    gameWidget = new GameWidget(this);
    battleWidget = new battle(this);

    // 添加到堆栈
    ui->stackedWidget->addWidget(startMenu);
    ui->stackedWidget->addWidget(pauseMenu);
    ui->stackedWidget->addWidget(gameWidget);
    ui->stackedWidget->addWidget(battleWidget);
}

void MainWindow::connectSignals()
{
    //连接快捷键
    connect(ui->actionExit, &QAction::triggered, this, &MainWindow::exitGame);
    connect(ui->actionAbout, &QAction::triggered, this, &MainWindow::showHelpUI);
    connect(ui->actionInitalMap,&QAction::triggered,this,[this](){
        gameWidget->MapSwitch("初始地图");
    });
    connect(ui->actionMap,&QAction::triggered,this,[this](){
        gameWidget->MapSwitch("Map");
    });

    // 连接开始菜单信号
    connect(startMenu, &StartMenuWidget::startGame, this, &MainWindow::showGameScreen);
    /*connect(startMenu, &StartMenuWidget::loadGame, this, &MainWindow::loadGame);*/
    connect(startMenu, &StartMenuWidget::helpShow, this, &MainWindow::showHelpUI);
    connect(startMenu, &StartMenuWidget::quitGame, this, &MainWindow::exitGame);

    //连接pause菜单
    connect(pauseMenu, &PauseMenuWidget::resumeGame, this, [this]() {
        ui->stackedWidget->setCurrentWidget(gameWidget);
        gameWidget->resumeGame();
    });
    connect(pauseMenu,&PauseMenuWidget::returnMainMenu,this,&MainWindow::showStartMenu);

    //连接battleWidget的信号
    connect(battleWidget,&battle::back,this,[this](){
        ui->stackedWidget->setCurrentWidget(gameWidget);
        gameWidget->resumeGame();
    });


    //连接gameWidget的信号
    connect(gameWidget,&GameWidget::pauseRequested,this,&MainWindow::showPauseMenu);
    connect(gameWidget,&GameWidget::wildBattle,this,[this](QVector<Pokemon *> &playerPokemon){

        //进战切换音乐
        bgmPlayer->stop();  // 先停止当前音乐
        bgmPlayer->setSource(QUrl("qrc:/bgm/music/bgm/fight.ogg"));  // 设置战斗音乐
        bgmPlayer->setLoops(QMediaPlayer::Infinite);  // 设置循环播放
        bgmPlayer->play();  // 播放战斗音乐

        //配队
        for(int i=0;i<playerPokemon.size();i++)
            battleWidget->addplayer(playerPokemon[i]);

        // 设置战斗界面的宝可梦
        battleWidget->setPlayerPokemon(playerPokemon[0]);

        battleWidget->addenemy(playerPokemon102);
        battleWidget->setEnemyPokemon(playerPokemon102);

        battleWidget->startBattle();
        ui->stackedWidget->setCurrentWidget(battleWidget);
        gameWidget->pauseGame();
    });
    connect(gameWidget,&GameWidget::npcBattle,this,[this](QVector<Pokemon *>& playerPokemon,QVector<Pokemon *>&npcPokemon){
        //进战切换音乐
        bgmPlayer->stop();  // 先停止当前音乐
        bgmPlayer->setSource(QUrl("qrc:/bgm/music/bgm/fight.ogg"));  // 设置战斗音乐
        bgmPlayer->setLoops(QMediaPlayer::Infinite);  // 设置循环播放
        bgmPlayer->play();  // 播放战斗音乐

        //配队
        for(int i=0;i<playerPokemon.size();i++)//玩家
            battleWidget->addplayer(playerPokemon[i]);
        for(int i=0;i<npcPokemon.size();i++)//npc
            battleWidget->addenemy(npcPokemon[i]);
        // 设置战斗界面的宝可梦
        battleWidget->setPlayerPokemon(playerPokemon[0]);//玩家
        battleWidget->setEnemyPokemon(npcPokemon[0]);//npc

        battleWidget->startBattle();
        ui->stackedWidget->setCurrentWidget(battleWidget);
        gameWidget->pauseGame();
    });

    //结束战斗恢复音乐
    connect(battleWidget, &battle::back, this, [this]() {
        bgmPlayer->stop(); // 先停止战斗音乐
        bgmPlayer->setSource(
            QUrl("qrc:/bgm/music/bgm/Junichi.ogg")); // 设置背景音乐
        bgmPlayer->setLoops(QMediaPlayer::Infinite); // 设置循环播放
        bgmPlayer->play();                           // 播放背景音乐

        ui->stackedWidget->setCurrentWidget(gameWidget);
        gameWidget->resumeGame();
    });

    //连接destroyed以清理所有技能与pokemon
    connect(this, &MainWindow::destroyed, this, [this]() {
        qDebug() << "destroyed";
        deletePokemon();
    });
}

void MainWindow::showStartMenu()
{
    ui->stackedWidget->setCurrentWidget(startMenu);

}

void MainWindow::showPauseMenu()
{
    ui->stackedWidget->setCurrentWidget(pauseMenu);
    //gameWidget->pauseGame();
}

void MainWindow::showHelpUI()
{
    aboutDialog->exec();
}

void MainWindow::exitGame()
{
    close();
}

void MainWindow::showGameScreen()
{
    ui->stackedWidget->setCurrentWidget(gameWidget);
    gameWidget->startGame();
}
